#include <nan.h>

namespace {
    // Native C++ function implementations
    void GetDetails(const Nan::FunctionCallbackInfo<v8::Value>& info) {
        // Your implementation for getDetails function
        // ...

        // For demonstration purposes, we print a message to the console
        printf("getDetails function called.\n");
    }

    void GetIsInstalled(const Nan::FunctionCallbackInfo<v8::Value>& info) {
        // Your implementation for getIsInstalled function
        // ...

        // For demonstration purposes, we print a message to the console
        printf("getIsInstalled function called.\n");
    }

    void InstallState(const Nan::FunctionCallbackInfo<v8::Value>& info) {
        // Your implementation for installState function
        // ...

        // For demonstration purposes, we print a message to the console
        printf("installState function called.\n");
    }

    void RunningState(const Nan::FunctionCallbackInfo<v8::Value>& info) {
        // Your implementation for installState function
        // ...

        // For demonstration purposes, we print a message to the console
        printf("installState function called.\n");
    }
    void LoadTimes(const Nan::FunctionCallbackInfo<v8::Value>& info) {
        // Your implementation for installState function
        // ...

        // For demonstration purposes, we print a message to the console
        printf("installState function called.\n");
    }

    void InitializeInstallStateObject(v8::Local<v8::Object> installStateObject) {
        // 向 InstallState 对象添加属性
        Nan::Set(installStateObject, Nan::New("DISABLED").ToLocalChecked(), Nan::New("disabled").ToLocalChecked());
        Nan::Set(installStateObject, Nan::New("INSTALLED").ToLocalChecked(), Nan::New("installed").ToLocalChecked());
        Nan::Set(installStateObject, Nan::New("NOT_INSTALLED").ToLocalChecked(), Nan::New("not_installed").ToLocalChecked());
    }

    void InitializeRunningStateObject(v8::Local<v8::Object> installStateObject) {
          // 向 InstallState 对象添加属性
          Nan::Set(installStateObject, Nan::New("CANNOT_RUN").ToLocalChecked(), Nan::New("cannot_run").ToLocalChecked());
          Nan::Set(installStateObject, Nan::New("READY_TO_RUN").ToLocalChecked(), Nan::New("ready_to_run").ToLocalChecked());
          Nan::Set(installStateObject, Nan::New("RUNNING").ToLocalChecked(), Nan::New("running").ToLocalChecked());
    }

    void InitializeAppObject(v8::Local<v8::Object> appObject) {
        // 创建 InstallState 对象
        v8::Local<v8::Object> installStateObject = Nan::New<v8::Object>();
        // 向 InstallState 对象添加属性
        InitializeInstallStateObject(installStateObject);
        // 向 app 对象添加 InstallState 对象
        Nan::Set(appObject, Nan::New("InstallState").ToLocalChecked(), installStateObject);

        // 创建 RunningState 对象
        v8::Local<v8::Object> runningStateObject = Nan::New<v8::Object>();
        // 向 InstallState 对象添加属性
        InitializeRunningStateObject(runningStateObject);
        // 向 app 对象添加 InstallState 对象
        Nan::Set(appObject, Nan::New("RunningState").ToLocalChecked(), runningStateObject);

        // 向字典对象添加函数属性
        Nan::Set(appObject, Nan::New("getDetails").ToLocalChecked(), Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GetDetails)).ToLocalChecked());
        Nan::Set(appObject, Nan::New("getIsInstalled").ToLocalChecked(), Nan::GetFunction(Nan::New<v8::FunctionTemplate>(GetIsInstalled)).ToLocalChecked());
        Nan::Set(appObject, Nan::New("installState").ToLocalChecked(), Nan::GetFunction(Nan::New<v8::FunctionTemplate>(InstallState)).ToLocalChecked());
        // 向 app 对象添加属性
        Nan::Set(appObject, Nan::New("isInstalled").ToLocalChecked(), Nan::New(false));
        Nan::Set(appObject, Nan::New("runningState").ToLocalChecked(), Nan::GetFunction(Nan::New<v8::FunctionTemplate>(RunningState)).ToLocalChecked());
    }

    void Initialize(v8::Local<v8::Object> exports) {
        Nan::HandleScope scope;

        // 创建一个字典对象
        v8::Local<v8::Object> chromeDictionary = Nan::New<v8::Object>();

        v8::Local<v8::Object> chromeAppDictionary = Nan::New<v8::Object>();
//         v8::Local<v8::Object> loadTimesObj = Nan::New<v8::Object>();
        InitializeAppObject(chromeAppDictionary);
        // 向字典对象添加对象属性
        Nan::Set(chromeDictionary, Nan::New("loadTimes").ToLocalChecked(), Nan::GetFunction(Nan::New<v8::FunctionTemplate>(LoadTimes)).ToLocalChecked());
        Nan::Set(chromeDictionary, Nan::New("app").ToLocalChecked(), chromeAppDictionary);

        // 向 exports 对象添加字典属性
        Nan::Set(exports, Nan::New("chrome").ToLocalChecked(), chromeDictionary);
    }
}  // namespace

// 初始化 Node.js 模块
void InitializeModule(v8::Local<v8::Object> exports, v8::Local<v8::Value> module, v8::Local<v8::Context> context) {
    Nan::HandleScope scope;
    Initialize(exports);
}

// 使用 NAN_MODULE_INIT 替代 NODE_MODULE
NAN_MODULE_INIT(NODE_GYP_chrome) {
    InitializeModule(target, target, v8::Isolate::GetCurrent()->GetCurrentContext());
}

NODE_MODULE(NODE_GYP_MODULE_NAME, NODE_GYP_chrome)
